
package com.haiheng.adscreen
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.haiheng.common.constant.AppConstant
import com.haiheng.common.utils.PowerOnOffHelper
import com.haiheng.commonlibrary.MainActivity
import com.haiheng.library_base.utils.SPUtils
import java.util.*

/**
 * 开机启动广播
 */
class BootRestartReceiver : BroadcastReceiver() {
    val ACTION = "android.intent.action.BOOT_COMPLETED"
    val TAG = "BootRestartReceiver"
    override fun onReceive(context: Context?, intent: Intent?) {
        Log.e(TAG, "开机启动........ ")
        val task: TimerTask = object : TimerTask() {
            override fun run() {
                val intent2 = Intent(context, MainActivity::class.java)
                intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                context!!.startActivity(intent2)
                setScheduledTask(context)
            }
        }
        val time = Timer()
        time.schedule(task,10000)
    }

    fun setScheduledTask(context: Context) {
        var taskString = SPUtils.getInstance().getString(AppConstant.SP_SCHEDULED_TASK)
        PowerOnOffHelper.setCurrentScheduledTimePair(context,taskString)
/*        if (taskString.isNotBlank()){
            try {
                var tasksBean = GsonUtils.fromJson(taskString, ScheduledTasksBean::class.java)
                if (tasksBean != null && tasksBean.services != null
                    && tasksBean.services.size > 0 && tasksBean.services.get(0).properties != null
                    && tasksBean.services.get(0).properties.size > 0
                    && tasksBean.services.get(0).properties.get(0).content != null
                    && tasksBean.services.get(0).properties.get(0).content.jobList != null
                    && tasksBean.services.get(0).properties.get(0).content.jobList.size > 0
                ) {
                    var jobList = tasksBean.services.get(0).properties.get(0).content.jobList
                    var timeOnTask: ScheduledTaskJob? = null
                    var timeOffTask: ScheduledTaskJob? = null
                    for (task in jobList) {
                        when (task.jobName) {
                            "TurnOn" -> {
                                timeOnTask = task
                            }
                            "TurnOff" -> {
                                timeOffTask = task
                            }
                        }
                    }
                    if (timeOnTask != null && timeOffTask != null) {
                        PowerOnOffHelper.setPowerOnOffYongHui(context, timeOnTask, timeOffTask)
                    }
                }
            }catch (ex:Exception){
                LogWriter.e("定时任务解析异常:${ex.message}")
            }

        }*/
    }
}